package org.rqb.robot;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.rqb.Main;
import org.rqb.entry.MyEvent;
import org.rqb.frame.GestureWindow;
import org.rqb.frame.MyFrame;
import org.rqb.panel.RepeatPanel;
import org.rqb.panel.partPanel.RepeatNumPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.List;

@Slf4j
@Data
public class MyRobot {
    private static MyRobot instance;
    private Robot robot;
    private MyFrame frame;
    private RepeatPanel repeatPanel;
    private boolean loop = false;
    private GestureWindow gestureWindow;

    private MyRobot() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }


    public static MyRobot getInstance(){
        if (instance == null){
            instance = new MyRobot();
        }
        return instance;
    }

    public MyRobot init(MyFrame frame, RepeatPanel repeatPanel){
        this.frame = frame;
        this.repeatPanel = repeatPanel;
        return instance;
    }


    public void executeEvents(List<MyEvent> events, int num) throws InterruptedException {
        if (gestureWindow == null ){
            gestureWindow = GestureWindow.getInstance();
        }
        RepeatNumPanel repeatNumPanel = gestureWindow.startRepeatNumPanel(0 + "/" + num);


        for (int i = 0; i < num; i++) {
            if (loop){ //控制是否跳出循环
                break;
            }
            for (MyEvent event : events) {

                try {
                    Thread.sleep(event.getTimeStamp()); //休眠间隔时间
                    switch (event.getType()) {
                        case MOUSE_MOVE:
                            robot.mouseMove(event.getX(), event.getY());
                            break;
                        case LEFT_MOUSE_DOWN:
                            robot.mouseMove(event.getX(), event.getY());
                            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                            break;
                        case LEFT_MOUSE_UP:
                            robot.mouseMove(event.getX(), event.getY());
                            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                            break;
                        case RIGHT_MOUSE_DOWN:
                            robot.mouseMove(event.getX(), event.getY());
                            robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                            break;
                        case RIGHT_MOUSE_UP:
                            robot.mouseMove(event.getX(), event.getY());
                            robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
                            break;
                        case MOUSE_WHEEL_UP:  // 处理向上滚轮事件
                            robot.mouseWheel(-1);  // 向上滚动
                            break;
                        case MOUSE_WHEEL_DOWN:  // 处理向下滚轮事件
                            robot.mouseWheel(1);   // 向下滚动
                            break;
                        case KEYDOWN:
                            robot.keyPress(event.getVkCode());
                            break;
                        case KEYUP:
                            robot.keyRelease(event.getVkCode());
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            int repeatNum = i + 1;
            repeatPanel.updateExecutedTimes(repeatNum);
            repeatNumPanel.setRepeatNum(repeatNum + "/" + num);
        }
        gestureWindow.stopRepeatNumPanel();
        repeatClosed();


    }

    /**
     * 复现关闭
     */
    public void repeatClosed() {
        loop = true;
        if (frame != null) {
            //展示myFrame，从最小化展示出来
           frame.displayAtTheForefront();
        }
        //复现流程结束，释放已经按下的键
        releaseKeys();
    }

    /**
     * 释放ctrl和Shift和Alt
     */
    private void releaseKeys() {
        //手动释放ctrl和shift
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_ALT);
    }





    /**
     * 切换窗口
     */
    public void switchWindows(){
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_ALT);
    }


    public void chickWin() {
        // 按下 Win 键
        robot.keyPress(KeyEvent.VK_WINDOWS);
        // 松开 Win 键
        robot.keyRelease(KeyEvent.VK_WINDOWS);
    }

    public void showDesk() {
        robot.keyPress(KeyEvent.VK_WINDOWS);
        // 按下 D 键
        robot.keyPress(KeyEvent.VK_D);
        // 先释放 D，再释放 Win
        robot.keyRelease(KeyEvent.VK_D);
        robot.keyRelease(KeyEvent.VK_WINDOWS);
    }


    public void click(int x,int y){
        robot.mouseMove(x,y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        log.info("点击一次");
    }

}
